<?php
/**
 * @author    zhuang <pingguoli@gmail.com>
 * @date      2020/07/03 11:09
 * @copyright Copyright 2018 - 2019
 *  
 *  
 * @version   1.0
 */

namespace App\Http\Controllers\Family;


use App\Http\Controllers\Controller;

use App\Models\Family;
use App\Policies\ChildPolicy;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;

/**
 * Class BaseController
 * @package App\Http\Controllers\Admin
 */
abstract class BaseController extends Controller
{
    /**
     * @var Family|null
     */
    private $_user = null;

    /**
     * 设置管理员
     * @param $family
     * @return $this
     */
    public function setUser($family)
    {
        $this->_user = $family;
        return $this;
    }

    /**
     * @return Family|null
     */
    public function getUser()
    {
        if ($this->_user === null) {
            $family = Auth::guard('family')->user();
            $this->_user = $family;
        }

        return $this->_user;
    }

    /**
     * 验证非权限下是否允许操作
     * @param $ability
     * @param Model $model
     * @return \Illuminate\Auth\Access\Response
     * @throws \Illuminate\Auth\Access\AuthorizationException
     */
    public function allow($ability, Model $model)
    {
        return $this->authorizeForUser($this->getUser(), $ability, $model);
    }
}